package com.xhj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhj.entity.Role;
import com.xhj.entity.UserRole;
import com.xhj.mapper.RoleMapper;
import com.xhj.service.RoleService;
import com.xhj.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2021-03-16 12:51:20
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper,Role> implements RoleService {

    @Autowired
    private UserRoleService userRoleService;

    @Override
    public Map<String, Object> findRoleByUserId(String userId) {
        // 查询所有的role
        List<Role> allRoleList = baseMapper.selectList(null);
        // 根据userId查询拥有的roleId
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<UserRole> userRoleList = userRoleService.list(queryWrapper);
        List<String> roleIdList = userRoleList.stream().map(c -> c.getRoleId()).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        //对角色进行分类
        List<Role> assignRoles = new ArrayList<Role>();
        for (Role role : allRoleList) {
            // 假如roleId存在于roleIdList
            if (roleIdList.contains(role.getId())) assignRoles.add(role);
        }
        map.put("assignRoles",assignRoles);
        map.put("allRoleList",allRoleList);
        return map;
    }

    @Override
    public void saveUserRoleRelationShip(String userId, String[] roleIds) {
        // 删除原先的用户角色关系
        userRoleService.remove(new QueryWrapper<UserRole>().eq("user_id", userId));
        List<UserRole> userRoleList = new ArrayList<>();
        for(String roleId : roleIds) {
            if(StringUtils.isEmpty(roleId)) continue;
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoleList.add(userRole);
        }
        userRoleService.saveBatch(userRoleList);
    }

    @Override
    public List<Role> selectRoleByUserId(String uid) {
        // 查询所有的role
        List<Role> allRoleList = baseMapper.selectList(null);
        // 根据userId查询拥有的roleId
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",uid);
        List<UserRole> userRoleList = userRoleService.list(queryWrapper);
        List<String> roleIdList = userRoleList.stream().map(c -> c.getRoleId()).collect(Collectors.toList());
        //对角色进行分类
        List<Role> roleList = new ArrayList<Role>();
        for (Role role : allRoleList) {
            // 假如roleId存在于roleIdList
            if (roleIdList.contains(role.getId())) roleList.add(role);
        }
        return roleList;
    }
}